package com.lcxw.hxzbadmin.controller.viplevelcontroller;

import com.alibaba.fastjson.JSONObject;
import com.lcxw.hxzbcommon.entity.ErrorCode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lcxw.hxzbapi.entity.pojo.VipLevel;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbapi.service.VipLevelService;
import com.lcxw.hxzbcommon.common.ObsUtil;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("admin/vipLevel")
public class VipLevelController {

    private ObsUtil obsUtil = new ObsUtil();

    @Resource
    VipLevelService vipLevelService;

    /**
     * 1、获取所有vip等级接口
     */
    @PostMapping("/getAll")
    public Response getAllVipLevel(@RequestBody JSONObject requestParams) {
        log.info("admin {} get all vipLevel", requestParams.getString("adminName"));
        List<VipLevel> vipLevels = vipLevelService.selectAll();
        Map<String, Object> data = new HashMap<>();
        data.put("vipLevels", vipLevels);
        return Response.ok(data);
    }

    /**
     * 2、添加/修改vip等级接口
     */
    @PostMapping("/mod")
    public Response modVipLevel(@RequestBody ModReqBean modReqBean) {
        log.info("admin {} mod vip level info.", modReqBean.getAdminName());
        List<VipLevel> vipLevels = modReqBean.getVipLevels();
        for (VipLevel viplevel : vipLevels) {
            if (null==viplevel.getId() || null == vipLevelService.selectByPrimaryKey(viplevel.getId())) {
                vipLevelService.insert(viplevel);
            } else {
                vipLevelService.updateById(viplevel);
            }
        }
        return Response.ok();

    }

    /**
     * 3、删除一个vipLevel
     */
    @PostMapping("/delete")
    public Response deleteVipLevel(@RequestBody JSONObject requestParams) {
        log.info("admin {} delete vip level: {}", requestParams.getString("adminName"),
            requestParams.getInteger("id"));
        int id = requestParams.getInteger("id");
        vipLevelService.deleteByPrimaryKey(id);
        return Response.ok();
    }

    /**
     * 4、上传vip等级相对应的图片
     */
    @PostMapping("/pic/upload")
    public Response uploadVipLevelPic(@RequestBody JSONObject requestParams){
        String name = requestParams.getString("name");
        String picName = requestParams.getString("picName");
        String imgOrigin = requestParams.getString("pic");
        String suffix = picName.substring(picName.lastIndexOf("."));
        String imgName = "vipLevelPic/" + name + "_vipLevel"+suffix;
        String picUrl;
        try {
            picUrl = obsUtil.uploadPic(imgName,imgOrigin);
        } catch (Exception e) {
            return Response.common(log,"上传VIP等级图片失败", ErrorCode.EXTERNAL_ERROR,"上传VIP等级图片失败",null);
        }

        return Response.ok(picUrl);
    }


    @Data
    static class ModReqBean {
        String adminName;
        String token;
        List<VipLevel> vipLevels;
    }
}
